/**
 * @(#)ApkConfigController.java 2016年12月1日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.MiguVideoRetentAddConfig;
import org.jsbd.boss.domian.MiguVideoRetentAddStatistics;
import org.jsbd.boss.service.IMiguVideoRetentAddConfigService;
import org.jsbd.boss.service.IMiguVideoRetentAddStatisticsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/miguVideoRetentAddConfig")
public class MiguVideoRetentAddConfigController {

	public static final Logger LOGGER = LoggerFactory.getLogger(MiguVideoRetentAddConfigController.class);

	@Resource
	private IMiguVideoRetentAddConfigService miguVideoRetentAddConfigService;

	@Resource
	private IMiguVideoRetentAddStatisticsService miguVideoRetentAddStatisticsService;

	@RequestMapping(value = "configList", method = RequestMethod.GET)
	public String configList() {
		return "/miguVideoRetentAddConfig/configList";
	}

	@RequestMapping(value = "getList")
	@ResponseBody
	public Map<String, Object> getList(Integer miguVideoId) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (miguVideoId != null) {
			MiguVideoRetentAddConfig miguVideoRetentAddConfig = new MiguVideoRetentAddConfig();
			miguVideoRetentAddConfig.setMiguVideoId(miguVideoId);
			List<MiguVideoRetentAddConfig> list = miguVideoRetentAddConfigService.getList(miguVideoRetentAddConfig);
			map.put("rows", list);
			map.put("total", 0);
		} else {
			List<MiguVideoRetentAddConfig> list = new ArrayList<>();
			list.add(new MiguVideoRetentAddConfig());
			map.put("rows", null);
			map.put("total", 0);
		}
		return map;
	}

	@RequestMapping(value = "/saveConfig")
	public void saveRetent(@RequestBody MiguVideoRetentAddConfig[] list, HttpServletResponse response) {

		if (list != null && list.length > 0) {
			Integer miguVideoId = list[0].getMiguVideoId();
			miguVideoRetentAddConfigService.deleteByMiguVideoId(miguVideoId);
		}
		miguVideoRetentAddConfigService.save(Arrays.asList(list));
		ResponseUtils.responseSuccess(response);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		try {
			List<Integer> list = Arrays.asList(ids);
			miguVideoRetentAddConfigService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/miguVideoRetentAddConfig/list";
	}
	
	@RequestMapping(value = "getListPage")
	@ResponseBody
	public Map<String, Object> getListPage(DataGridModel page,
			MiguVideoRetentAddStatistics miguVideoRetentAddStatistics) {
		String sname = miguVideoRetentAddStatistics.getSname();
		if (StringUtils.isNotBlank(sname)) {
			String[] snames = sname.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : snames) {
				keywords.add(keyword);
			}
			miguVideoRetentAddStatistics.setKeywords(keywords);
		}
		String execDate = miguVideoRetentAddStatistics.getExecDate();
		if (StringUtils.isNotBlank(execDate)) {
			miguVideoRetentAddStatistics.setExecDate(execDate.replaceAll("-", ""));
		}
		return miguVideoRetentAddStatisticsService.getListPage(page, miguVideoRetentAddStatistics);
	}

}
